<template>
  <div :class="$style.chapter">
    <div v-if="chapterList.length > 0">
      <div v-for="(c, i) in chapterList" :key="i + c.chapterTitle">
        <h1 :class="$style.chapterDesc">
          {{ charCn(i) }}、{{ c.chapterTitle }}
          <span v-if="!isPreview" :class="$style.btn">
            <Chapter :id="c.id" type="text" title="编辑" :chapter-title="c.chapterTitle" :index="c.serialNumber" :questionnaire-id="questionnaireId" @on-success="onRefreshHandle"></Chapter>
            <Button type="text" @click="removeHandle(c.id)">删除</Button>
          </span>
        </h1>
        <div :class="$style.question">
          <PreviewQuestion :editable="editable" :is-preview="isPreview" :chapter-id="c.id" :questionnaire-id="questionnaireId" :question-list="c.questionnaireQuestionDTOList" @on-refresh="onRefreshHandle"></PreviewQuestion>
          <Question v-if="!isPreview" :questionnaire-id="questionnaireId" :chapter-id="c.id" :index="c.questionnaireQuestionDTOList.length+1" @on-refresh="onRefreshHandle"></Question>
        </div>
      </div>
    </div>
    <slot></slot>
  </div>
</template>
<script>
import { charCn } from '../common'
import PreviewQuestion from './previewQuestion'
import Chapter from './chapter'
import Question from './question'
import { questionnaireItemDelete } from '@/api/questionnaire'
export default {
  name: 'ChapterList',
  components: {
    PreviewQuestion,
    Question,
    Chapter
  },
  props: {
    chapterList: {
      type: Array,
      default: () => []
    },
    questionnaireId: {
      type: String,
      default: ''
    },
    editable: {
      type: Boolean,
      default: true
    },
    isPreview: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {}
  },
  methods: {
    charCn,
    // 编辑标题
    onRefreshHandle(id) {
      this.$emit('on-refresh')
    },
    // 删除标题
    removeHandle(id) {
      this.$msgbox({
        title: '提示',
        message: '确认删除？',
        showCancelButton: true,
        type: 'warning'
      }).then(() => {
        questionnaireItemDelete({ id }).then(res => {
          this.$message.success('删除成功！')
          this.$emit('on-refresh')
        })
      })
    }
  }
}
</script>
<style lang="scss" module>
.chapter {
  // border: 1px solid rgb(220, 223, 230);
  // border-radius: 8px;
  padding: 10px;
  .chapterDesc {
    font-size: 20px;
    margin: 0 0 10px 0;
  }
  .question {
    padding: 10px;
    border: 1px solid rgb(220, 223, 230);
    border-radius: 8px;
    margin-bottom: 10px;
    &:hover {
      border: 1px solid rgba(0, 0, 0, 0.45);
    }
  }
  .btn{
    font-weight: normal;
    float: right;
    & [class*="el-button--text"]{
      padding: 0 5px 0 0 !important;
    }
  }
}
</style>
